package com.dds.system.service.admin.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.mapper.admin.AdminUserOperationChildMapper;
import com.dds.system.domain.admin.AdminUserOperationChild;
import com.dds.system.service.admin.IAdminUserOperationChildService;

/**
 * 运营中心员工Service业务层处理
 * 
 * @author gaolianjie
 * @date 2021-04-14
 */
@Service
public class AdminUserOperationChildServiceImpl implements IAdminUserOperationChildService 
{
    @Autowired
    private AdminUserOperationChildMapper adminUserOperationChildMapper;

    /**
     * 查询运营中心员工
     * 
     * @param id 运营中心员工ID
     * @return 运营中心员工
     */
    @Override
    public AdminUserOperationChild selectAdminUserOperationChildById(Long id)
    {
        return adminUserOperationChildMapper.selectAdminUserOperationChildById(id);
    }

    /**
     * 查询运营中心员工列表
     * 
     * @param adminUserOperationChild 运营中心员工
     * @return 运营中心员工
     */
    @Override
    public List<AdminUserOperationChild> selectAdminUserOperationChildList(AdminUserOperationChild adminUserOperationChild)
    {
        return adminUserOperationChildMapper.selectAdminUserOperationChildList(adminUserOperationChild);
    }

    /**
     * 新增运营中心员工
     * 
     * @param adminUserOperationChild 运营中心员工
     * @return 结果
     */
    @Override
    public int insertAdminUserOperationChild(AdminUserOperationChild adminUserOperationChild)
    {
        return adminUserOperationChildMapper.insertAdminUserOperationChild(adminUserOperationChild);
    }

    /**
     * 修改运营中心员工
     * 
     * @param adminUserOperationChild 运营中心员工
     * @return 结果
     */
    @Override
    public int updateAdminUserOperationChild(AdminUserOperationChild adminUserOperationChild)
    {
        return adminUserOperationChildMapper.updateAdminUserOperationChild(adminUserOperationChild);
    }

    /**
     * 批量删除运营中心员工
     * 
     * @param ids 需要删除的运营中心员工ID
     * @return 结果
     */
    @Override
    public int deleteAdminUserOperationChildByIds(Long[] ids)
    {
        return adminUserOperationChildMapper.deleteAdminUserOperationChildByIds(ids);
    }

    /**
     * 删除运营中心员工信息
     * 
     * @param id 运营中心员工ID
     * @return 结果
     */
    @Override
    public int deleteAdminUserOperationChildById(Long id)
    {
        return adminUserOperationChildMapper.deleteAdminUserOperationChildById(id);
    }

    @Override
    public List<Map<String, Object>> selectAllUserList(String name) {
        return adminUserOperationChildMapper.selectAllUserList(name);
    }

    /**
     * 获取运营人员比例
     * @param serveUser 服务商
     */
    public List<Map<String, Object>> getOperationProportion(String serveUser) {
        AdminUserOperationChild adminUserOperationChild = adminUserOperationChildMapper.selectAdminUserOperationChildByUserId(serveUser);

        List<Map<String, Object>> dataLst = new ArrayList<>();

        dataLst.add(getDataMap(adminUserOperationChild, 1, BigDecimal.ZERO));

        // v1.0版运营中心
        if (adminUserOperationChild.getDepartmentId() == 0) {
            if (adminUserOperationChild.getLevel() != 1) {
                dataLst = getData(adminUserOperationChild.getParentId(), dataLst, adminUserOperationChild.getShareProportion());
            }

            return dataLst;
        }

        return null;
    }

    /**
     * 获取v1.0数据
     * @param parentId
     * @param dataLst
     * @param shareProportion
     * @return
     */
    private List<Map<String, Object>> getData(Long parentId, List<Map<String, Object>> dataLst, BigDecimal shareProportion) {
        AdminUserOperationChild parentInfo = adminUserOperationChildMapper.selectAdminUserOperationChildById(parentId);

        dataLst.add(getDataMap(parentInfo, 0, shareProportion));

        if (parentInfo.getLevel() != 1) {
            return getData(parentInfo.getParentId(), dataLst, parentInfo.getShareProportion());
        }

        return dataLst;
    }

    /**
     * 拼接分润data数据
     * @param info 服务商
     * @param isOwner 是否所属
     * @return
     */
    private Map<String, Object> getDataMap(AdminUserOperationChild info, int isOwner, BigDecimal shareProportion) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("userId", info.getUserId());
        dataMap.put("proportion", info.getShareProportion().subtract(shareProportion));
        dataMap.put("level", info.getLevel());
        dataMap.put("isOwner", isOwner);

        return dataMap;
    }
}
